//
//  TestQueuePoolViewController.swift
//  QueueToolBox
//
//  Created by kingdee on 2021/11/25.
//

import UIKit

class TestQueuePoolViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
//        testSerialPool()
        testConcurrentPool()
    }
    
    fileprivate func testSerialPool() {
        
        let queue1 = HHQueuePool.pool.serialQueue(name: "name1", qos: DispatchQoS.userInitiated)
        let queue2 = HHQueuePool.pool.serialQueue(name: "name2", qos: DispatchQoS.userInitiated)
        let queue3 = HHQueuePool.pool.serialQueue(name: "name3", qos: DispatchQoS.userInitiated)
//        let queue3 = HHQueuePool.pool.serialQueue(name: "name3", qos: DispatchQoS.default)
        
        queue1.async {
            debugPrint("queue1 task1 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue1 task1 end")
        }
        queue1.async {
            debugPrint("queue1 task2 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue1 task2 end")
        }
        
        queue3.async {
            debugPrint("queue3 task1 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue3 task1 end")
        }
        
        queue2.sync {
            debugPrint("queue2 task1 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue2 task1 end")
        }
        
        queue3.async {
            debugPrint("queue3 task2 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue3 task2 end")
        }
        
        queue2.sync {
            debugPrint("queue2 task2 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue2 task2 end")
        }
        
    }
    
    fileprivate func testConcurrentPool() {
        let queue1 = HHQueuePool.pool.concurrentQueue(name: "name1", qos: DispatchQoS.userInitiated)
        let queue2 = HHQueuePool.pool.concurrentQueue(name: "name2", qos: DispatchQoS.userInitiated)
        let queue3 = HHQueuePool.pool.concurrentQueue(name: "name3", qos: DispatchQoS.userInitiated)
//        let queue3 = HHQueuePool.pool.concurrentQueue(name: "name3", qos: DispatchQoS.default)
        
        queue1.async {
            debugPrint("queue1 task1 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue1 task1 end")
        }
        queue1.async {
            debugPrint("queue1 task2 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue1 task2 end")
        }
        
        queue3.async {
            debugPrint("queue3 task1 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue3 task1 end")
        }
        
        queue2.sync {
            debugPrint("queue2 task1 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue2 task1 end")
        }
        
        queue3.async {
            debugPrint("queue3 task2 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue3 task2 end")
        }
        
        queue2.sync {
            debugPrint("queue2 task2 start")
            for _ in 0...1000000 {
                autoreleasepool {
                    let date = Date()
                }
            }
            debugPrint("queue2 task2 end")
        }
    }
    
}
